package com.arcvideo.acsplayer.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.arcvideo.acsplayer.R;

public class TitleView extends FrameLayout {

	public static final int STATE_FORWORD = 1;
	public static final int STATE_BACKWORD = 2;

	private Context mContext = null;

	private ImageButton mLeftButton = null;
	private ImageButton mRightButton = null;
	private ImageButton mMiddleButton = null;
	private TextView mTitleView = null;

	private int mLeftId = 0;
	private int mMiddleId = 0;
	private int mRightId = 0;

	private int mRotationType = 0;
	private boolean mbVRMode = false;

	private TitleInterface mTitleInterface = null;

	/**
	 * 回调函数
	 *
	 * @author skl5250
	 *
	 */
	public interface TitleInterface {
		public void clickLeft();

		public void clickMiddel();

		public void clickRight();
	}

	/**
	 * 按键点击事件
	 */
	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			switch (view.getId()) {
				case R.id.id_title_left_btn :
					if (null != mTitleInterface)
						mTitleInterface.clickLeft();

					break;
				case R.id.id_title_middle_btn :
					if (null != mTitleInterface)
						mTitleInterface.clickMiddel();

					break;

				case R.id.id_title_rigth_btn :
					if (null != mTitleInterface)
						mTitleInterface.clickRight();

					break;
				default :
					break;
			}
		}
	};

	/**
	 * 构造函数
	 *
	 * @param context
	 */
	public TitleView(Context context) {
		super(context);
		mContext = context;
	}

	/**
	 * 构造函数
	 *
	 * @param context
	 * @param attributeSet
	 */
	public TitleView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		mContext = context;
	}

	/**
	 * 初始化
	 *
	 * @param rotationType
	 * @param leftId
	 * @param isVRMode
	 * @param titleInterface
	 */
	public void init(int rotationType, int leftId, boolean isVRMode, TitleInterface titleInterface) {
		mRotationType = rotationType;
		mLeftId = leftId;
		mbVRMode = isVRMode;
		mTitleInterface = titleInterface;
		initUI();
	}

	/**
	 * 初始化
	 *
	 * @param rotationType
	 * @param leftId
	 * @param rigthId
	 * @param isVRMode
	 * @param titleInterface
	 */
	public void init(int rotationType, int leftId, int rigthId, boolean isVRMode, TitleInterface titleInterface) {
		mRotationType = rotationType;
		mLeftId = leftId;
		mRightId = rigthId;
		mbVRMode = isVRMode;
		mTitleInterface = titleInterface;
		initUI();
	}

	/**
	 * 初始化
	 *
	 * @param rotationType
	 * @param leftId
	 * @param middleId
	 * @param rigthId
	 * @param isVRMode
	 * @param titleInterface
	 */
	public void init(int rotationType, int leftId, int middleId, int rigthId, boolean isVRMode, TitleInterface titleInterface) {
		mRotationType = rotationType;
		mLeftId = leftId;
		mMiddleId = middleId;
		mRightId = rigthId;
		mbVRMode = isVRMode;
		mTitleInterface = titleInterface;
		initUI();
	}

	/**
	 * 初始化UI
	 */
	public void initUI() {
		LayoutInflater.from(mContext).inflate(R.layout.actionbar_title, this);
		mLeftButton = (ImageButton) findViewById(R.id.id_title_left_btn);
		mLeftButton.setOnClickListener(mOnClickListener);
		mMiddleButton = (ImageButton) findViewById(R.id.id_title_middle_btn);
		mMiddleButton.setOnClickListener(mOnClickListener);
		mRightButton = (ImageButton) findViewById(R.id.id_title_rigth_btn);
		mRightButton.setOnClickListener(mOnClickListener);
		mTitleView = (TextView) findViewById(R.id.id_title_text_view);

		if (mLeftId != 0) {
			mLeftButton.setVisibility(VISIBLE);
			mLeftButton.setBackgroundResource(mLeftId);
		} else {
			mLeftButton.setVisibility(GONE);
		}

		if (mMiddleId != 0) {
			mMiddleButton.setVisibility(VISIBLE);
			mMiddleButton.setBackgroundResource(mMiddleId);
		} else {
			mMiddleButton.setVisibility(GONE);
		}

		if (mRightId != 0) {
			mRightButton.setVisibility(VISIBLE);
			mRightButton.setBackgroundResource(mRightId);
		} else {
			mRightButton.setVisibility(GONE);
		}
		reload(mRotationType, mbVRMode);

	}

	/**
	 * 重新加載
	 *
	 * @param rotationType
	 * @param isVRMode
	 */
	public void reload(int rotationType, boolean isVRMode) {
		mRotationType = rotationType;
		mbVRMode = isVRMode;

		if (mbVRMode) {
			mRightButton.setVisibility(VISIBLE);
			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			lp.addRule(RelativeLayout.LEFT_OF, R.id.id_title_rigth_btn);
			lp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
			lp.rightMargin = 40;
			mMiddleButton.setLayoutParams(lp);
			if (mRotationType == Configuration.ORIENTATION_PORTRAIT) {
				mMiddleButton.setBackgroundResource(R.drawable.btn_vr_p);
			} else {
				mMiddleButton.setBackgroundResource(R.drawable.btn_vr_h_p);
			}

		} else {
			mRightButton.setVisibility(INVISIBLE);
			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) new RelativeLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
			lp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
			lp.rightMargin = 40;
			mMiddleButton.setLayoutParams(lp);
			if (mRotationType == Configuration.ORIENTATION_PORTRAIT) {
				mMiddleButton.setBackgroundResource(R.drawable.btn_vr_n);
			} else {
				mMiddleButton.setBackgroundResource(R.drawable.btn_vr_h_n);
			}
		}
	}

	/**
	 * 设置标题
	 *
	 * @param titleName
	 */
	public void setTitle(String titleName) {
		mTitleView.setText(titleName);
	}

	/**
	 * 得到标题
	 *
	 */
	public String getTitle() {
		return mTitleView.getText().toString();
	}

	/**
	 * 设置VR模式
	 *
	 * @param rotationType
	 * @param isVRMode
	 */
	public void setVRMode(int rotationType, boolean isVRMode) {
		mRotationType = rotationType;
		mbVRMode = isVRMode;
		if (mbVRMode) {
			if (mRotationType == Configuration.ORIENTATION_PORTRAIT) {
				mMiddleButton.setBackgroundResource(R.drawable.btn_vr_p);
			} else {
				mMiddleButton.setBackgroundResource(R.drawable.btn_vr_h_p);
			}
		} else {
			if (mRotationType == Configuration.ORIENTATION_PORTRAIT) {
				mMiddleButton.setBackgroundResource(R.drawable.btn_vr_n);
			} else {
				mMiddleButton.setBackgroundResource(R.drawable.btn_vr_h_n);
			}
		}
	}

	/**
	 * 显示
	 */
	public void show() {
		setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_top_in));
		setVisibility(View.VISIBLE);
	}

	/**
	 * 隐藏
	 */
	public void hide() {
		setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_top_out));
		setVisibility(View.INVISIBLE);
	}

}